package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import com.shop2cn.iapi.sellerpromotion.parameter.*;
import com.shop2cn.iapi.sellerpromotion.service.*;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.database.mongo.Shop2cnSellerPromotionInfoWapper;
import com.ymttest.utils.YMTDateUtil;
public class Shop2cnSellerPromotionCallService {
	 private static String AMOUNT_SUBTRACT_TEMPLATE = "满%s减%s";
	 private static String AMOUNT_DISCOUNT_TEMPLATE = "满%s享%s折";
	 private static String QTY_SUBTRACT_TEMPLATE = "满%s件减%s";
	 private static String QTY_DISCOUNT_TEMPLATE = "满%s件享%s折";

	/**
	 * 创建满减call
	 * @param createpromotionBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @return promotionId
	 * @throws InterruptedException 
	 */
	public static Integer CreatepromotionCall(CreatePromotionBean createpromotionBean) throws ClientProtocolException, IOException, InterruptedException {
		CreatePromotionCall createpromotionCall=new CreatePromotionCall();
		createpromotionCall.setData(createpromotionBean);
		createpromotionCall.callService();
		Integer promotionId=createpromotionCall.getJsonObject().getAsJsonObject().get("data").getAsJsonObject().get("promotionId").getAsInt();
		Thread.sleep(100);
		return promotionId;
	}
	/**
	 * 根据条件获取文字描述
	 * @param type
	 * @param condition
	 * @param discount
	 * @return
	 */
	public static String getValue(Integer type,BigDecimal condition,BigDecimal discount) {
		String template="";
		//type 1：满件减2：满件折3：满额减4：满额折   
		if(type==1) {
			template=QTY_SUBTRACT_TEMPLATE;
		}else if(type==2) {
			template=QTY_DISCOUNT_TEMPLATE;
			discount=discount.multiply(BigDecimal.valueOf(10));
		}
		else if(type==3) {
			template=AMOUNT_SUBTRACT_TEMPLATE;
		}
		else if(type==4) {
			template=AMOUNT_DISCOUNT_TEMPLATE;
			discount=discount.multiply(BigDecimal.valueOf(10));
		}
//		else if(type==5)
//		{
//			template=FREEFREIGHT_ALL_TEMPLATE;	
//		}
//		else if(type==6)
//			template=FREEFREIGHT_ANY_TEMPLATE;	
		
		return String.format(template, condition, discount);
	}
	/**
	 * 根据时间获取状态
	 * @param begin YMDHMS = "yyyy-MM-dd HH:mm:ss";
	 * @param end YMDHMS = "yyyy-MM-dd HH:mm:ss";
	 * @return
	 */
	public static Integer getStatus(String bt, String et) {
        LocalDateTime begin=YMTDateUtil.getLocalDateTime(bt, YMTDateUtil.YMDHMS);
        LocalDateTime end=YMTDateUtil.getLocalDateTime(et, YMTDateUtil.YMDHMS);
        if (end.isBefore(LocalDateTime.now())) {
            return 2;
        }
		if (begin.isAfter(LocalDateTime.now())) {
            return 0;
        }
        return 1;
    }
	/**
	 * 用promotionId查询状态
	 * @param promotionId
	 * @return 状态-1：全部0：未开始1：已开始2：已结束
	 * @throws Exception 
	 * 注意：这个方法不会过滤已删除
	 */
	public static Integer getStatus(Integer promotionId) throws Exception {
		Map m = new Shop2cnSellerPromotionInfoWapper().getPromotionById(promotionId);
        LocalDateTime begin=YMTDateUtil.getLocalDateTime(YMTDateUtil.getFromDTSDate(m.get("bt").toString(), YMTDateUtil.YMDHMS), YMTDateUtil.YMDHMS);
        LocalDateTime end=YMTDateUtil.getLocalDateTime(YMTDateUtil.getFromDTSDate(m.get("et").toString(), YMTDateUtil.YMDHMS), YMTDateUtil.YMDHMS);
        if (end.isBefore(LocalDateTime.now())) {
            return 2;
        }
		if (begin.isAfter(LocalDateTime.now())) {
            return 0;
        }
        return 1;
    }
	/**
	 * 结束
	 * @param mchId
	 * @param promotionId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void PromotionEnd(Integer mchId,Integer promotionId) throws ClientProtocolException, IOException {
		PromotionEndBean promotionendBean=new PromotionEndBean();
		PromotionEndCall promotionendCall=new PromotionEndCall();
		promotionendBean.setPromotionId(promotionId);
		promotionendBean.setSellerId(mchId); 
		promotionendCall.setData(promotionendBean);
		promotionendCall.callService();	
	}
	/**
	 * 删除Promotion
	 * @param mchId
	 * @param promotionId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void PromotionDelete(Integer mchId,Integer promotionId) throws ClientProtocolException, IOException {
		PromotionEnd(mchId, promotionId);
		PromotionDeleteBean PromotionDeleteBean=new PromotionDeleteBean();
		PromotionDeleteCall PromotionDeleteCall=new PromotionDeleteCall();
		PromotionDeleteBean.setPromotionId(promotionId);
		PromotionDeleteBean.setSellerId(mchId); 
		PromotionDeleteCall.setData(PromotionDeleteBean);
		PromotionDeleteCall.callService();	
	}
	/**
	 * 获取mchId下的所有未结束的PromotionId 用来批量结束（limit500）
	 * @param mchId
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static List<Integer> getEnablePromotion(Integer mchId) throws ClientProtocolException, IOException {
		List<Integer> promotionIdlist = new ArrayList<Integer>();
		PromotionListBean promotionlistBean = new PromotionListBean();
		PromotionListCall promotionlistCall = new PromotionListCall();
		promotionlistBean.setSellerId(mchId);
		promotionlistBean.setStatus(-1);
		promotionlistBean.setPageIndex(1);
		promotionlistBean.setPageSize(500);
		promotionlistCall.setData(promotionlistBean);
		promotionlistCall.callService();
		List<PromotionListVo> list = promotionlistCall.getPromotionList();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				PromotionListVo pvo = list.get(i);
				// 查出全部未开始和进行中的
				if (pvo.getStatus() == 0 || pvo.getStatus() == 1) {
					promotionIdlist.add(pvo.getPromotionId());
				}
			}
		}
		return promotionIdlist;
	}
	/**
	 * 结束用户下的全部活动
	 * @param mchId
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void EndAllEnablePromotion(Integer mchId) throws ClientProtocolException, IOException {
		List<Integer> enablelist = getEnablePromotion(mchId);
		if(enablelist!=null||enablelist.size()>0) {
		enablelist.forEach(p->{
			try {
				PromotionEnd(mchId, p);
			} catch (Exception e) {
//				e.printStackTrace();
			}
		});
		}
	}
	/**
	 * 创建一个默认满减
	 * @param mchId 商家id
	 * @param type 1：满件减2：满件折3：满额减4：满额折
	 * @param promotionItemList 满减规则
	 * @return
	 */
	public static CreatePromotionBean  initCreatePromotionBean(Integer mchId,Integer type,List<PromotionItem> promotionItemList) {
		CreatePromotionBean CreatePromotionBean=initCreatePromotionBean(mchId, type);
		CreatePromotionBean.setPromotionItemList(promotionItemList);
		return CreatePromotionBean;
	}
	/**
	 * 创建一个默认满减
	 * @param mchId 商家id
	 * @param type 1：满件减2：满件折3：满额减4：满额折
	 * @return
	 */
	public static CreatePromotionBean  initCreatePromotionBean(Integer mchId,Integer type) {
		CreatePromotionBean CreatePromotionBean=new CreatePromotionBean();
		CreatePromotionBean.setBegin(YMTDateUtil.getBeforeOrNextDay(0,"yyyy-MM-dd HH:mm:ss"));
		CreatePromotionBean.setEnd(YMTDateUtil.getBeforeOrNextDay(1,"yyyy-MM-dd HH:mm:ss"));
		
		CreatePromotionBean.setCurrency("CNY");
		CreatePromotionBean.setName("AutoTest满减"+type);
		
		//不输入全场
		CreatePromotionBean.setProductIdList(null);
		List<PromotionItem> promotionItemList=new ArrayList<PromotionItem>();
		PromotionItem e=new PromotionItem();
		e.setLevel(1);
		e.setUseCondition(BigDecimal.valueOf(100));
		if(type==1||type==3) {
			//减10元
			e.setValue(BigDecimal.valueOf(10));
		}else {
			//9折
			e.setValue(BigDecimal.valueOf(9));
		}
		promotionItemList.add(e);
		CreatePromotionBean.setPromotionItemList(promotionItemList);
		CreatePromotionBean.setSameProductUse(true);
		CreatePromotionBean.setSellerId(mchId);
		CreatePromotionBean.setType(type);
		return CreatePromotionBean;
	}
	public static void main(String[] args) throws Exception {

	}
}
